<?php
		/**
		 * @author huxiaomin 2011 03 14
		 * @example $dao = DDb::Dao();
		 */
		class DDb
		{
			static $all_dao = array();
			static $slave_dao = array();
			
			/**
			 * 
			 * @param  $string  'dbschema=cyz&dbuser=root&dbpass=&dbhost=127.0.0.1&dbport=3306'
			 */
			static function Dao($string = '')
			{
				    
					if ($string == '')
					{
						$string = D_DAO;
					}
					if (isset( self::$all_dao[$string] ) )
					{
						$db = self::$all_dao[$string];
						$db->ping();
						return $db;
					}else {
						parse_str($string);
						$db = new DDAO();
						$db->Connect( $dbschema , $dbuser, $dbpass ,$dbhost , $dbport );
						return self::$all_dao[$string] = $db;
						
					}
			}
			/**
			 * 
			 * @param  $string  'dbschema=cyz&dbuser=root&dbpass=&dbhost=127.0.0.1&dbport=3306'
			 */
			static function Dao_DEFAULT($string = '')
			{
				    
					if ($string == '')
					{
						$string = D_DAO_DEFAULT;
					}
					if (isset( self::$all_dao[$string] ) )
					{
						$obj = self::$all_dao[$string];
						$obj->ping();
						return $obj;
					}else {
						parse_str($string);
						$db = new DDAO();
						$db->Connect( $dbschema , $dbuser, $dbpass ,$dbhost , $dbport );
						return self::$all_dao[$string] = $db;
						
					}
			}
			
			
			static function Dao_Slave(){
			   if (self::$slave_dao){
			   	 $obj = self::$slave_dao;
			   	 $obj->ping();
			     return $obj;
			   }     
			   if (defined('D_DAO_SLAVE')){
			      $slaves = json_decode(D_DAO_SLAVE, 1);
			      $key = array_rand($slaves, 1);
			      parse_str($slaves[$key]);
			      $db = new DDAO();
                  $db->Connect( $dbschema , $dbuser, $dbpass ,$dbhost , $dbport );
                  return self::$slave_dao = $db;
			   }else {
			      return self::Dao();
			   }
			}
			
		}


		class DDAO
		{
			private $schema;
			private $connection;
			public $assoc = true;
			static $error = array();
			static $sql_log = array();
			
			function __construct()
			{
				
			}
			
			/**
			 * 连接数据库
			 * @param $dbschema
			 * @param $dbuser
			 * @param $dbpass
			 * @param $dbhost
			 * @param $dbport
			 */
			function Connect( $dbschema = '', $dbuser = '', $dbpass = '',$dbhost = '127.0.0.1', $dbport = '3306' )
			{
				$this->connection = mysql_pconnect($dbhost . ':' . $dbport, $dbuser, $dbpass);
				if ( $this->connection != false )
				{
					$this->schema = $dbschema;
					mysql_select_db($this->schema, $this->connection);
				}else{
					$this->cal_error('connect error:'.$dbhost . ':' . $dbport.','.$dbuser.','.$dbpass);
				}
				mysql_query("SET NAMES utf8;", $this->connection);
			}
			
			function ping(){
			     mysql_ping($this->connection);
			}
			
			/**
		     * 取一个值
		     * @access public
		     * @param string $sql example : select field_a from table_a Limit 1
		     * @return mixed
		     */
			function GetValue($sql){
				$temp = $this->assoc;
				$this->assoc = false;
				$row = $this->GetRow($sql);	
				$this->assoc = $temp;
				if ( $row === false ){
				    return false;
				}
				return $row[0];
				
			}
			/**
		     * 取一行(一维数组)
		     * @access public
		     * @param string $sql example : select field_a from table_a Limit 1
		     * @return array| false
		     */
			function GetRow($sql,$assoc=NULL){
				$rs = $this->Query($sql);
				if ( $rs === false ){
				    return false;
				}
				$temp = $this->assoc;
				if ($assoc !== NULL){
                    $this->assoc = $assoc;
				}
				$row = $this->fa($rs);
				$this->assoc = $temp;
				mysql_free_Result($rs);
				return $row;
			}
			/**
		     * 取一列(一维数组)
		     * @access public
		     * @param string $sql sql语句
		     * @return array
		     */
			function GetCol($sql){
				$rs = $this ->Query($sql);
				if ( $rs === false ){
				    return false;
				}
				$data = array();
				$temp = $this->assoc;
				$this->assoc = false;
				while( ($row = $this ->fa($rs)) != false ){
					$data[] = $row[0];
				}
				$this->assoc = $temp;
				mysql_free_Result($rs);
				return $data;
			}
			/**
		     * 取多行(二维数组)
		     * @access public
		     * @param string $sql
		     * @return array
		     */
			function GetPlan($sql,$assoc=NULL){
				$rs = $this ->Query($sql);
				if ( $rs === false ){
				    return false;
				}
			    $temp = $this->assoc;
                if ($assoc !== NULL){
                    $this->assoc = $assoc;
                }		
				$data = array();
				while( ($row = $this ->fa($rs)) != false ){
					$data[] = $row;
				}
				$this->assoc = $temp;
				mysql_free_Result($rs);
				return $data;
			}
			
			
			function GetPlan2($sql,$pri0,$pri1="",$assoc=NULL) {
				$rs = $this ->Query($sql);	
				if ( $rs === false ){
				    return false;
				}
			    $temp = $this->assoc;
                if ($assoc !== NULL){
                    $this->assoc = $assoc;
                }
				$data = array();
				if($pri1) {
					while( ($r = $this->fa($rs)) != false ) {
						$data[$r[$pri0]][$r[$pri1]] = $r;
					}
				} else {
					while( ($r = $this->fa($rs)) != false ) {
						$data[$r[$pri0]] = $r;
					}
				} 
				$this->assoc = $temp;
				mysql_free_Result($rs);
				return $data;
				
			}
			/**
		    * 执行一条查询命令
		    * @access private
		    * @param string $sql sql
		    * @return resource|boolean
		    */
			function Query($sql){
				if (DEBUG_TYPE){
					self::$sql_log[] = $sql;   
				}
				$res =  mysql_query($sql, $this->connection);
				if ($res === false)
				{
					$this->cal_error($sql);
				}
			    return $res;
			}
			/**
			 * @return mysql_affected_rows
			 */
			function Update($sql)
			{
				$res =  mysql_query($sql, $this->connection);
				if ($res === false)
				{
					$this->cal_error($sql);
					return false;
				}
				$res = mysql_affected_rows($this->connection);
			    return $res;
			}
			/**
			 * 插入新的id
			 */
			function LastId()
			{
				return mysql_insert_id($this->connection);
			}
			
			
			
			/**
			* 取一行
			* @access private
			* @param resource $rs 结果集
			* @return array
			*/
			function fa($rs){
				if(!$this->assoc)
				{
					return mysql_fetch_row($rs);
				}
				else
				{
					return mysql_fetch_assoc($rs);
				}
			}
			 /**
		     * 记录错误sql的位置
		     * @param $sql
		     */
		    function cal_error($sql)
		    {
		    	self::$error[] = array($sql,mysql_error($this->connection));
		    }
		}
		 